package date1023;

import org.apache.commons.io.FileUtils;
import org.apache.xmlbeans.impl.tool.CodeGenUtil;
import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

/**
 * @Author: huhx
 * @Date: 2017-10-23 下午 4:34
 */
public class DatagramGenerate {

    private final static String REQUEST_STRING = "<elementAlias name=\"{0}\"  alias=\"{1}\" /><!-- {2} -->";
    private final static String RESPONSE_STRING = "<xmlTag tagName=\"{0}\"><String name=\"{1}\"></String></xmlTag><!-- {2} -->";
    private final static String TABLE_STRING = "\t{0} AS {1},";

    @Test
    public void generateRequestDocs() throws Exception {
        File file = new File("file/request.txt");
        List<String> strings = FileUtils.readLines(file);
        for (String string : strings) {
            String[] strArrays = string.split("\\s");
            String alias = strArrays[0];
            String comment = strArrays[1];
            String name = GenerateUtils.getWithHumpContent(alias);
            System.out.println(MessageFormat.format(REQUEST_STRING, name, alias, comment));
        }
        System.out.println("-----------------------------------------------");
        for (String string : strings) {
            String[] strArrays = string.split("\\s");
            String tagName = strArrays[0];
            String comment = strArrays[1];
            String name = GenerateUtils.getWithHumpContent(tagName);
            System.out.println(MessageFormat.format(RESPONSE_STRING, tagName, name, comment));
        }
    }

    @Test
    public void generateResponseDocs() throws Exception {
        File file = new File("file/response.txt");
        List<String> strings = FileUtils.readLines(file);
        for (String string : strings) {
            String[] strArrays = string.split("\\s");
            String tagName = strArrays[0];
            String comment = strArrays[1];
            String name = GenerateUtils.getWithHumpContent(tagName);
            System.out.println(MessageFormat.format(RESPONSE_STRING, tagName, name, comment));
        }
        System.out.println("-----------------------------------------------");
        for (String string : strings) {
            String[] strArrays = string.split("\\s");
            String alias = strArrays[0];
            String comment = strArrays[1];
            String name = GenerateUtils.getWithHumpContent(alias);
            System.out.println(MessageFormat.format(REQUEST_STRING, name, alias, comment));
        }
    }

    @Test
    public void generateMysqlSelectDocs() throws Exception {
        System.out.println("SELECT");
        File file = new File("file/table.txt");
        List<String> strings = FileUtils.readLines(file);
        for (String string : strings) {
            String column = string.split("\\s")[0];
            System.out.println(MessageFormat.format(TABLE_STRING, column, GenerateUtils.transforFirstLetterLower(column)));
        }
    }

    // 插入数据库字段的语句
    @Test
    public void generateInsertDocs() {
        String requestDatagram = "INSERT INTO\n\t{0}({1})\nVALUES\n\t({2})";
        File file = new File("file/insert.txt");
        try {
            List<String> listStrings = FileUtils.readLines(file);
            String tablesName = listStrings.get(0);
            String baseSql = "";
            String mapSql = "";
            String string = "";
            for (int i = 1; i < listStrings.size(); i++) {
                string = listStrings.get(i).split(" ")[0];
                baseSql += string + ", ";
                mapSql += "#{" + GenerateUtils.transforFirstLetterLower(string) + "}, ";
            }
            baseSql = baseSql.substring(0, baseSql.length() - 2);
            mapSql = mapSql.substring(0, mapSql.length() - 2);
            String line = MessageFormat.format(requestDatagram, tablesName.toUpperCase(), baseSql, mapSql);
            System.out.println(line);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
